<!--
 * @Author: your name
 * @Date: 2020-08-25 17:56:27
 * @LastEditTime: 2020-08-28 14:33:47
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ryfront\src\modules\roles\Res.vue
-->
<template>
    <div>
        <el-row>
            <el-col :span="24">
                <el-tree ref="tree" node-key="resourceId" :props="props" default-expand-all show-checkbox :data="treeData">

                </el-tree>
            </el-col>
        </el-row>
        <el-row style="margin-top:20px;">
            <el-col :span="24">
                <el-button size="small" type="primary" @click="saveRes"  >确定</el-button>
                <el-button size="small" @click="closeDialog" >取消</el-button>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import {roleresByRoleId,resListAll,roleresSaveByRoleId} from '@/api/index';

export default {
    name: 'role-res',
    props:{
        roleId:{
            type:String,
            default: '',
        }
    },
    data() {
        return {
            props:{
                children:'children',
                label:'resourceName'
            },
            treeData:[],

        }
    },
    mounted() {
        

        this.getAllRes();
    },
    methods: {
        //关闭弹出框
        closeDialog(){
            this.$emit('closeDialog',"res");
        },
        //角色ID
        getResByRoleId(roleId){
            roleresByRoleId(roleId).then((result) => {
                if(result.status == 200){
                    // setCheckedNodes
                    let list = result.data.data;
                    let selNode = [];
                    for(let i = 0; i < list.length;i++){
                        selNode.push(list[i].resourceId);
                    }
                    this.$refs.tree.setCheckedKeys(selNode);
                }
            });
        },
        getAllRes(){
            resListAll().then((result) => {
                if(result.status == 200){
                    let list = result.data.data;
                    //遍历该树，从而执行
                    //根节点
                    let rootobj = list[0];
                    rootobj.children = [];                    
                    for(let i=1;i<list.length;i++){
                        if(list[i].resourceParent = rootobj.resourceId){
                            rootobj.children.push(list[i]);
                        }
                    }
                    this.treeData.push(rootobj);
                    this.getResByRoleId(this.roleId);
                }
            })
        },
        //保存资源
        saveRes(){
            //获取选中的节点  resourceId 数组            
            if(this.$refs.tree.getCheckedKeys().length != 0){
                let obj = {
                    roleId: this.roleId,
                    ress: this.$refs.tree.getCheckedKeys(),
                }
                roleresSaveByRoleId(obj).then((result) => {
                    if(result.status == 200){
                        this.$message({
                            type:"success",
                            message:"批量添加成功"
                        });
                        this.closeDialog();
                    }
                })
            }
        }

    },
    watch: {
        
    },
}
</script>

<style scoped>

</style>